/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hymnal.church;

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kvsevolo
 */
public class TableManager {
    String melodyTableFile;
    static TreeMap tableHymns;
    TableManager(String melodyTableFileArg) {
        melodyTableFile = melodyTableFileArg;
    }
    public static class TableItem extends Object {

        public int commonNum;
        public int englishNum;
        public String firstLine;
        public String melodyFileNameUrl = "";
        public String melodyFileName = "";
        TableItem() {
            
        }
        @Override
        public String toString() {
            String string = "Ru num:" + commonNum + "\n"
                    + "Eng num:" + englishNum + "\n"
                    + "F-line:" + firstLine + "\n"
                    + "Melody file URL:" + melodyFileNameUrl + "\n"
                    + "Melody file:" + melodyFileName;
            return string;
            
        }
    }
    static int getItemsCount() {
        return tableHymns.size();
    }
    static TableItem getItem(int i) {
        return (TableItem)tableHymns.get(i);
    }
    
    static void printSystemOutAllTable() {
        for (int i = 0; i < tableHymns.size(); i++) {
            TableItem item = (TableItem) tableHymns.get(i);
            System.out.println(item);
        }
    }

    static public TableItem createTableItem() {
        TableItem item = new TableItem();
        return item;
    }

    static {
        tableHymns = new TreeMap();
    }
    
    static void saveTableFile(String tableFile) {
        File f = new File(tableFile);
        FileWriter fw = null;
        try {
            fw = new FileWriter(f);

            BufferedWriter bw = new BufferedWriter(fw);



            int count = TableManager.getItemsCount();
            for (int i = 1; i <= count; i++) {
                TableItem item = (TableItem) getItem(i);
                String line = item.commonNum + ":"
                        + item.englishNum + ":"
                        + item.firstLine + ":"
                        + item.melodyFileName;
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            bw.close();
        } catch (IOException ex) {
            System.out.println("saveTableFile:" + ex.getMessage());
        }
    }
}
